package com.atguigu.gulimall.product.controller;

import java.util.Arrays;
import java.util.Map;
import java.util.List;

import com.atguigu.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.entity.vo.AttrGroupRelationVo;
import com.atguigu.gulimall.product.entity.vo.AttrGroupWithAttrVO;
import com.atguigu.gulimall.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrGroupService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;



/**
 * 属性分组
 *
 * @author xuweixing
 * @email xuweixing@gmail.com
 * @date 2021-05-08 17:02:39
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService relationService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 查询组关联的属性
     */
    @RequestMapping("/{attrgroupId}/attr/relation")
    public R listAttr(@PathVariable("attrgroupId") Long attrGroupId){
        //根据属性组id查询所有属性
        List<AttrEntity> attrs = attrService.getRelationAttr(attrGroupId);

        return R.ok().put("data", attrs);
    }

    /**
     * 添加属性与分组关联关系
     */
    @RequestMapping("/attr/relation")
    public R attrRelation(@RequestBody List<AttrGroupRelationVo> vos){
        //根据属性组id查询所有属性
        relationService.saveRelationAttr(vos);

        return R.ok();
    }

    /**
     * 查询分类下的所有分组级分组下的属性
     */
    @RequestMapping("/{catelogId}/withattr")
    public R attrgroupWithAttr(@PathVariable("catelogId") Long catelogId){
        //根据属性组id查询所有属性
        List<AttrGroupWithAttrVO> attrGroups = attrGroupService.attrgroupWithAttrByCatelogId(catelogId);

        return R.ok().put("data",attrGroups);
    }

    /**
     * 查询组没有关联的属性
     */
    @RequestMapping("/{attrgroupId}/noattr/relation")
    public R listNoAttr(@PathVariable("attrgroupId") Long attrGroupId,
                        @RequestParam Map<String, Object> params){
        //根据属性组id查询所有没有关联的属性
        PageUtils page = attrService.getNoRelationAttr(attrGroupId,params);

        return R.ok().put("page", page);
    }

    /**
     * 根据分类Id查询列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("catelogId") Long catelogId){
        PageUtils page = attrGroupService.queryPage(params,catelogId);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        Long catelogId = attrGroup.getCatelogId();
        Long [] catelogPath = categoryService.getCatelogPath(catelogId);

		attrGroup.setCatelogPath(catelogPath);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/attr/relation/delete")
    public R deleteAttrRelation(@RequestBody AttrGroupRelationVo[] vos){
//        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));
        relationService.removeRelation(vos);

        return R.ok();
    }

}
